<?php

namespace app\api\controller;

use app\common\model\Likes;
use app\common\model\UserScoreCount;
use think\Cache;
use think\Db;

class Post extends \app\common\controller\Api
{
    protected $noNeedLogin = ['list','details','column'];
    protected $noNeedRight = '*';
    //发布话题
    public function index(){
        $params = $this->request->post();
        // 表单验证
        $this->shoproValidate($params, get_class(), 'index');
        $user=$this->auth->getUser();
//        if(!$count=Cache::get('post_count_'.$user->id)){
//            Cache::set('post_count_'.$user->id,0,strtotime('23:59:59')-time());
//        }
//        if($count>config('site.post_number')){
//            $this->error('今日创作已用完');
//        }
        if(UserScoreCount::getTodayCount($user->id,'post')<=0){
            $this->error('今日创作已用完');
        }
        Db::startTrans();
        try{
            $topic=\app\common\model\Topic::where('id',$params['topic_ids'])->find();
            if(!$topic){
                $this->error('话题不存在');
            }
            $post= \app\common\model\Post::create([
                'content'=> $params['content'],
                'images'=> empty($params['images'])?null:$params['images'],
                'videofiles'=> empty($params['videofiles'])?null:$params['videofiles'],
                'user_id'=>$user['id'],
                'category_id'=>$params['category_id'],
                'topic_ids'=>$params['topic_ids'],
                'type'=>$params['type'],
                'phone'=>$params['phone'],
                'province_id'=>$params['province_id'],
                'city_id'=>$params['city_id'],
                'area_id'=>$params['area_id'],
                'city'=>$params['city'],
                'media_type'=>$params['media_type']
            ]);
            $topic->setInc('number');
            //线上积分记录
//            Cache::inc('post_count_'.$user->id);
            UserScoreCount::setTodayCount($user->id,'post');
            Db::commit();
        } catch (\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
        }
            $this->success('发布成功',$post);
    }
    //帖子列表
    public function list(){
        $type=$this->request->get('type','all');
        $category_id=$this->request->get('category_id');
        $topic_ids=$this->request->get('topic_ids');
        $search=$this->request->get('search');
        $query=\app\common\model\Post::with(['user'])->show()->field('id,content,images,videofiles,user_id,category_id,topic_ids,type,city,status,media_type')->where('column_id',0);
        if($type=='hot'){
            //推荐的
            $query->hot();
        }
        if($category_id){
            //分类标签
            $query->where('category_id',$category_id);
        }
        if($topic_ids){
            //话题
            $query->where('topic_ids',$topic_ids);
        }
        if($search){
            //搜索
            $query->where('content','like',"%{$search}%");
        }
        $this->success('帖子列表',$query->paginate());
    }
    //帖子详情
    public function details($id){
        $post=\app\common\model\Post::with(['user','topic','category'])->find($id);
        if ( $user= \app\common\model\User::info()){
            $user=\app\common\model\User::find($user->id);
            if(!$count=Cache::get('post_view_count_'.$user->id)){
                $count= Cache::set('post_view_count_'.$user->id,0,strtotime('23:59:59')-time());
            }

//            if($count<config('site.post_view_number')){
            if(UserScoreCount::getTodayCount($user->id,'post_view')>0){
                \addons\shopro\model\User::score(config('site.post_view_score'),$user,'post_view');
                UserScoreCount::setTodayCount($user->id,'post_view');
//                Cache::inc('post_view_count_'.$user->id);
                //线上积分记录
//                $user->setInc('post_score',config('site.post_score'));
            }
        }
        if(!$post){
            $this->error('帖子不存在');
        }
        $this->success('帖子列表',$post);
    }
    //帖子点赞/取消点赞
    public function like($id){
        $post=\app\common\model\Post::with(['user','topic','category'])->find($id);
        if(!$post){
            $this->error('帖子不存在');
        }
        $user=$this->auth->getUser();
        if(Likes::isLike($id)){
            Likes::where(['user_id'=>$user->id,'type'=>'post','like_id'=>$id])->delete();
            $this->success('取消成功','cancel');
        }else{
            Likes::create(['user_id'=>$user->id,'type'=>'post','like_id'=>$id]);
            $this->success('点赞成功','like');
        }
    }
    //投稿记录
    public function myList(){
        $user=$this->auth->getUser();
        $list=\app\common\model\Post::with(['user','topic','category'])-> where(['user_id'=>$user->id])->field('id,content,images,videofiles,user_id,category_id,topic_ids,type,city,status,media_type')->paginate();
        $this->success('投稿记录',$list);
    }
    //专栏帖子
    public function column(){
        $column_id=$this->request->get('column_id');
        $query=\app\common\model\Post::with(['user'])->show()->field('id,content,images,videofiles,user_id,category_id,topic_ids,type,city,status,media_type');
        if($column_id){
            //专题标签
            $query->where('column_id',$column_id);
        }
        $this->success('专题帖子列表',$query->paginate());
    }
}